import {observable, action, runInAction} from 'mobx';
import dateUtils from '../utils/DateUtils'
import {Modal, Toast} from 'antd-mobile';

const alert = Modal.alert;

/**
 * mobx 状态管理类，所有的逻辑处理都在这里
 * */
class CarPayStore {
    @observable body; //支付需要的信息
    @observable payType = 'wx_app'; //支付类型
    @observable payments = 0;//付款金额
    @observable actualPayments = 0;//实际付款金额
    @observable discount; //赠送折扣
    @observable discountType; //赠送类型
    @observable attach = '';
    @observable attach2 = '';
    @observable detail = '';
    @action
    init = (props) => {
        runInAction(() => {
            this.body = props.location.state.body;
            this.payments = props.location.state.payments;
            this.payType = 'wx_app';
            this.attach = this.body.plateNumber;
            this.attach2 = this.body.parkId;
            this.detail = JSON.stringify({
                months: this.body.months,
                renewExpirationTime: this.body.renewExpirationTime
            });
        });
        Toast.loading('Loading...', 0);
        postJson("/car/discount/queryDiscountMax.x", {
            parkId: this.body.parkId,
            month: this.body.months
        }, (data) => {
            console.log("data", data);
            runInAction(() => {
                Toast.hide();
                this.discountType = data.discountType;
                this.discount = data.discount;
                if (data.discountType && data.discountType === 'DISCOUNT_DIS' && data.discount) {
                    this.actualPayments = this.payments * data.discount;
                } else {
                    if (data.discountType && data.discountType === 'DISCOUNT_GIVE' && data.discount) {
                        console.log("renewExpirationTime", dateUtils.addDayDate(this.body.renewExpirationTime, data.discount * 30))
                    }
                    this.actualPayments = this.payments;
                }
            });
        }, (error) => {
            console.log("error", error);
            Toast.hide();
        });
    };

    @action
    onChangePayType = (payType) => {
        runInAction(() => {
            this.payType = payType;
        });
    };

    paySubmit = (props) => {
        let params = {
            payType: "wx_h5",
            body: '车辆月卡缴费',
            orderType: "CAR_MONTH",
            totalFee: 0.01,
            payableFee: this.payments,
            ownerCode: ownerCode,
            commCode: commCode,
            attach: this.body.plateNumber,
            attach2: this.body.parkId,
            detail: this.detail
        };
        postJson("/api/v2/pay/anon/weixin/H5/order", params, (data) => {
            /*props.history.push('/pages/mobile/car/car.html?ownerCode='
                + ownerCode
                + "&commCode=" + commCode
                + "&commName=" + commName
                + "&httpToken=" + httpToken);*/
            Toast.loading('加载中...', 2);
            window.location.href = data.webUrl;
        }, (data) => {
            Toast.info(data.data ? data.data : data, 2, null, false);
        });
    };
}

export default new CarPayStore()